package com.baohua.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baohua.dao.SupplierDao;
import com.baohua.exception.BaohuaSystemException;
import com.baohua.model.Supplier;
import com.baohua.service.SupplierService;
import com.baohua.util.BeanUtils;

@Service("supplierService")
public class SupplierServiceImpl implements SupplierService {


	@Override
	public Map querySupplier(Supplier supplier, int page, int pageSize) {
		
		Map map = new HashMap();
		StringBuffer sqlCount = new StringBuffer("select count(*) from Supplier s where 1=1");
		StringBuffer sql = new StringBuffer("from Supplier s where 1=1");
		if(!StringUtils.isEmpty(supplier.getSupplierNO())) {
			sql.append(" and s.supplierNO = '" + supplier.getSupplierNO() + "'");
			sqlCount.append(" and s.supplierNO = '" + supplier.getSupplierNO() + "'");
		}
		if(!StringUtils.isEmpty(supplier.getSupplierName())) {
			sql.append(" and s.supplierName = '" + supplier.getSupplierName() + "'");
			sqlCount.append(" and s.supplierName = '" + supplier.getSupplierName() + "'");
		}
		
		List<Supplier> list = supplierDao.querySupplier(sql.toString(),(page-1)*pageSize, pageSize);
		long count = supplierDao.countSupplier(sqlCount.toString());
		map.put("Rows", list);
		map.put("Total", count);
		
		return map;
	}

	@Override
	public Supplier querySupplierBySupplierNo(String supplierNO) throws BaohuaSystemException {
		if(supplierNO == null) {
			throw new BaohuaSystemException("员工编号不能为空");
		}
		Supplier supplier = supplierDao.querySupplierBySupplierNo(supplierNO);
		return supplier;
	}


	@Override
	public void addSupplier(Supplier supplier) throws BaohuaSystemException {
		Supplier s = supplierDao.querySupplierBySupplierNo(supplier.getSupplierNO());
		if(s == null) {
			supplierDao.addSupplier(supplier);
		}else{
			throw new BaohuaSystemException("员工已经存在");
		}
	}

	@Override
	public void delSupplier(int supplierId) throws BaohuaSystemException {
		Supplier s = supplierDao.querySupplierBySupplierId(supplierId);
		if(s!=null) {
			supplierDao.delSupplier(s);
		}else {
			throw new BaohuaSystemException("此员工不存在");
		}
	}

	@Override
	public void updateSupplier(Supplier supplier) throws BaohuaSystemException {
		   Supplier s = supplierDao.querySupplierBySupplierId(supplier.getSupplierID());
			if(s!=null) {
				if(!BeanUtils.compareObject(s, supplier)) {
					BeanUtils.copyProperty(supplier, s);
					supplierDao.updateSupplier(s);
			   }
		}else {
			throw new BaohuaSystemException("此员工已经存在");
		}
	}

	@Override
	public long countSupplier(String sql) {
		return supplierDao.countSupplier(sql);
	}

	@Override
	public Supplier querySupplierById(int supplierId) throws BaohuaSystemException {
		Supplier r = supplierDao.querySupplierBySupplierId(supplierId);
		if(r == null) {
			throw new BaohuaSystemException("员工不存在");
		}
		return r;
	}

	
   private SupplierDao supplierDao;


	public SupplierDao getSupplierDao() {
		return supplierDao;
	}
	
	@Resource(name="supplierDao")
	public void setSupplierDao(SupplierDao supplierDao) {
		this.supplierDao = supplierDao;
	}
	
}
